<?php
namespace app\admin\controller;
use app\user\controller\User;
use think\Controller;
use think\Db;
use app\admin\model\AuthRule;
use app\admin\model\AuthGroup;

/**
 * 用户管理控制器
 * Class Member
 * @package app\admin\controller
 */
class Member extends Authinfo
{
	//用户列表
	public function index_member(){
		//模糊查询
		$map=array();
		$nickname=input('nickname');
		if($nickname){
			$map[]=array('nickname','like',$nickname.'%');
		}
		$company_name=input('company_name');
		if($company_name){
			$map[]=array('company_id','like','%'.$company_name.'%');
		}
		//分页
		$current=input('current');
		if(!$current){
			$current=1;
		}
		$pagenums=input('pagenums');
		if(!$pagenums){
			$pagenums=50;
		}
		//排序
		$nowpagenums=($current-1)*$pagenums;
		$order=input('order');
		if(!$order){
			$order='id DESC';
		}
		//数据查询
		if($map){
			$total=Db::name('Member')->where($map)->count('id');
			$info=Db::name('Member')->where($map)->order($order)->limit($nowpagenums,$pagenums)->select();
		}else{
			$total=Db::name('Member')->count('id');
			$info=Db::name('Member')->order($order)->limit($nowpagenums,$pagenums)->select();
		}
		//时间数据格式转换
		if($info){
			foreach ($info as $key=>$value){
				//通过部门id链表department 获取部门名称
				$info[$key]['company_name']=Db::name('Department')->field('name,id')->where(['name'=>$value['company_id']])->find();
				//数据格式转换
				$info[$key]['last_login_time']=time_nianyueri($value['last_login_time']);
				$info[$key]['create_time']=time_nianyueri($value['create_time']);
				$info[$key]['last_login_ip']=long2ip($value['last_login_ip']);
				if($value['status']==1){
					$info[$key]['status']='是';
				}else{
					$info[$key]['status']='否';
				}
			}
			$datas['status']=1;
			$datas['info']=$info;
			$datas['total']=$total;
			$datas['current']=$current;
		}else{
			$datas['status']=0;
			$datas['info']='无数据！';
			$datas['total']=$total;
			$datas['current']=$current;
		}
		return json($datas);

	}

	//用户添加
	public function  add_member(){
		$data=[];
		$datas=[];
		$username=input('username');
		$password=input('password');
		$mobile=input('mobile');
//		$company_id=input('company_id');
		$company=input('company_id');
//		$company_id=Db::name('Department')->where(['id'=>$company])->value('name');
		$email=input('email');
		$sfid=input('sfid');
		$nickname=input('nickname');
		$status=input('status');
		if(!$email){
			$email=$sfid.'@xuni.com';
		}

		if($username && $password){
			$User   =   new User();
			$uids    =   $User->register($username, $password, $email,'','admin',$nickname);
			if($uids){
				$data['mobile']=$mobile;
				$data['company_id']=$company;
				$data['sfid']=$sfid;
				$data['status']=!empty($status)?$status:'0';
				$data['create_time']=time();
				$gx=Db::name('Member')->where('id',$uids)->update($data);
				if($gx){
					$datas['status']=1;
					$datas['info']='新增成功！';
				}else{
					$datas['status']=0;
					$datas['info']=$uids;
				}

			}else{
				$datas['status']=0;
				$datas['info']='操作失败！';
			}
		}else{
			$datas['status']=0;
			$datas['info']='参数缺少！';
		}
		return json($datas);
	}

	//修改回显
	public function edit_view(){
		$id=input('id');
		$list=Db::name('Member')->field('id,username,nickname,company_id,mobile,email,status')->where(['id'=>$id])->find();
		if($list){
			$datas['status']=1;
			$datas['info']=$list;
		}else{
			$datas['status']=0;
			$datas['info']='无数据!';
		}
		return json($datas);
	}


	//用户修改
	public function edit_member(){
		$data=[];
		$datas=[];
		$id=input('id');
		$password=input('password');
		$status=!empty(input('status'))?input('status'):'0';
		$username=!empty(input('username'))?input('username'):'';
		$company=input('company_id');
		$sfid=!empty(input('sfid'))?input('sfid'):'';
		$nickname=!empty(input('nickname'))?input('nickname'):'';
		$mobile=input('mobile');
		$email=input('email');
		if($id){
			if ($password != "") {
				$data['password']=hb_member_md5($password,HB_AUTH_KEY);
			}
			$data['sfid']=$sfid;
			$data['username']=$username;
			$data['company_id']=$company;
			$data['nickname']=$nickname;
			$data['email']=$email;
			$data['status']=$status;
			$data['update_time']=time();

			$gx=Db::name('Member')->where('id',$id)->update($data);
			if($gx){
				$datas['status']=1;
				$datas['info']='操作成功！';
			}else{
				$datas['status']=0;
				$datas['info']='操作失败！';
			}
		}else{
			$datas['status']=0;
			$datas['info']='缺少参数！';
		}



		$psw=Db::name('Member')->where(['id'=>$id])->value('password');
		if(empty($password)){
			$data['password']=$psw;
		}else{
			$data['password']=$password;
		}



		if($mobile){
			$data['mobile']=$mobile;
		}
		$email=input('email');
		if(!$email){
			$email=$sfid.'@xuni.com';
		}

//		$password=!empty(input('password'))?input('password'):input('password');


		return  json($datas);

	}

	//用户删除
	public function delete_member(){
		$ids=input('id');
		if(is_array($ids)){
			$ids=Db::name('Member')->where('id','in',$ids)->delete();
			if($ids){
				$datas['status']=1;
				$datas['info']='操作成功！';
			}else{
				$datas['status']=0;
				$datas['info']='操作失败！';
			}
		}else{
			$datas['status']=0;
			$datas['info']='缺少参数！';
		}
		return  json($datas);
	}
	//部门数据
	public function list_member(){
		$list=Db::name('Department')->field('id,pid,name')->where('pid',0)->select();
		foreach($list as $k => $v){
			$list[$k]['son']=Db::name('Department')->where('pid',$v['id'])->field('id,pid,name')->select();
		}

		if($list){
			$datas['status']=1;
			$datas['info']=$list;
		}else{
			$datas['status']=0;
			$datas['info']='无数据！';
		}
		return json($datas);
	}
	//修改密码
	public function  changepassword1(){
		$data=[];
		$datas=[];
		$id=input('id');
		$password=input('password');
		if($id && $password){
           $data['update_time']=time();
           $data['password']=hb_member_md5($password, HB_AUTH_KEY);
           $gx=Db::name('Member')->where('id',$id)->update($data);
			if($gx){
			    $datas['status']=1;
			    $datas['info']='操作成功！';
			}else{
				$datas['status']=0;
				$datas['info']='操作失败！';
			}
        }else{
			$datas['status']=0;
			$datas['info']='缺少参数！';
        }
        return  json($datas);
    }
	//密码初始化
	public function changepassword(){
		$ids=input('id');
		if(is_array($ids)){
			$data['update_time']=time();
			$data['password']=hb_member_md5('123456', HB_AUTH_KEY);
			$gx=Db::name('Member')->where('id','in',$ids)->update($data);
			if($gx){
				$datas['status']=1;
				$datas['info']='操作成功！';
			}else{
				$datas['status']=0;
				$datas['info']='操作失败！';
			}
		}else{
			$datas['status']=0;
			$datas['info']='缺少参数！';
		}
		return  json($datas);
	}
}
